home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / internet / other / ka9q / ka9q_src.arc / FTPSERV.C < prev    next >
Encoding:
C/C++ Source or Header  |  1988-07-28  |  16.2 KB  |  689 lines

  1. /* FTP Server state machine - see RFC 959 */
  2.  
  3. #define    LINELEN        128    /* Length of command buffer */
  4.  
  5. #include <stdio.h>
  6. #include "global.h"
  7. #include "mbuf.h"
  8. #include "netuser.h"
  9. #include "timer.h"
  10. #include "tcp.h"
  11. #include "ftp.h"
  12.  
  13. /* Command table */
  14. static char *commands[] = {
  15.     "user",
  16. #define    USER_CMD    0
  17.     "acct",
  18. #define    ACCT_CMD    1
  19.     "pass",
  20. #define    PASS_CMD    2
  21.     "type",
  22. #define    TYPE_CMD    3
  23.     "list",
  24. #define    LIST_CMD    4
  25.     "cwd",
  26. #define    CWD_CMD        5
  27.     "dele",
  28. #define    DELE_CMD    6
  29.     "name",
  30. #define    NAME_CMD    7
  31.     "quit",
  32. #define    QUIT_CMD    8
  33.     "retr",
  34. #define    RETR_CMD    9
  35.     "stor",
  36. #define    STOR_CMD    10
  37.     "port",
  38. #define    PORT_CMD    11
  39.     "nlst",
  40. #define    NLST_CMD    12
  41.     "pwd",
  42. #define    PWD_CMD        13
  43.     "xpwd",            /* For compatibility with 4.2BSD */
  44. #define    XPWD_CMD    14
  45.     "mkd ",
  46. #define    MKD_CMD        15
  47.     "xmkd",            /* For compatibility with 4.2BSD */
  48. #define    XMKD_CMD    16
  49.     "xrmd",            /* For compatibility with 4.2BSD */
  50. #define    XRMD_CMD    17
  51.     "rmd ",
  52. #define    RMD_CMD        18
  53.     NULLCHAR
  54. };
  55.  
  56. /* Response messages */
  57. static char banner[] = "220 %s FTP version %s ready at %s\r\n";
  58. static char badcmd[] = "500 Unknown command\r\n";
  59. static char givepass[] = "331 Enter PASS command\r\n";
  60. static char logged[] = "230 Logged in\r\n";
  61. static char typeok[] = "200 Type OK\r\n";
  62. static char deleok[] = "250 File deleted\r\n";
  63. static char mkdok[] = "200 MKD ok\r\n";
  64. static char delefail[] = "550 Delete failed\r\n";
  65. static char pwdmsg[] = "257 \"%s\" is current directory\r\n";
  66. static char badtype[] = "501 Unknown type \"%s\"\r\n";
  67. static char badport[] = "501 Bad port syntax\r\n";
  68. static char unimp[] = "502 Command not yet implemented\r\n";
  69. static char bye[] = "221 Goodbye!\r\n";
  70. static char nodir[] = "553 Can't read directory \"%s\"\r\n";
  71. static char cantopen[] = "550 Can't read file \"%s\"\r\n";
  72. static char sending[] = "150 Opening data connection for %s %s\r\n";
  73. static char cantmake[] = "553 Can't create \"%s\"\r\n";
  74. static char portok[] = "200 Port command okay\r\n";
  75. static char rxok[] = "226 File received OK\r\n";
  76. static char txok[] = "226 File sent OK\r\n";
  77. static char noperm[] = "550 Permission denied\r\n";
  78. static char noconn[] = "425 Data connection refused\r\n";
  79. static char notlog[] = "530 Please log in with USER and PASS\r\n";
  80.  
  81. static struct tcb *ftp_tcb;
  82.  
  83. /* Start up FTP service */
  84. ftp_start(argc,argv)
  85. int argc;
  86. char *argv[];
  87. {
  88.     struct socket lsocket;
  89.     void ftpscr(),ftpscs();
  90.  
  91.     lsocket.address = ip_addr;
  92.     if(argc < 2)
  93.         lsocket.port = FTP_PORT;
  94.     else
  95.         lsocket.port = atoi(argv[1]);
  96.  
  97.     ftp_tcb = open_tcp(&lsocket,NULLSOCK,TCP_SERVER,0,ftpscr,NULLVFP,ftpscs,0,(char *)NULL);
  98. }
  99. ftp_stop()
  100. {
  101.     if(ftp_tcb != NULLTCB)
  102.         close_tcp(ftp_tcb);
  103. }
  104. /* FTP Server Control channel State change upcall handler */
  105. static
  106. void
  107. ftpscs(tcb,old,new)
  108. struct tcb *tcb;
  109. char old,new;
  110. {
  111.     extern char hostname[],version[];
  112.     struct ftp *ftp,*ftp_create();
  113.     void ftp_delete();
  114.     char *inet_ntoa();
  115.     long t;
  116.     char *cp,*cp1;
  117.  
  118.     switch(new){
  119. /* Setting QUICKSTART piggybacks the server's banner on the SYN/ACK segment;
  120.  * leaving it unset waits for the three-way handshake to complete before
  121.  * sending the banner. Piggybacking unfortunately breaks some old TCPs,
  122.  * so its use is not (yet) recommended.
  123. */
  124. #ifdef    QUICKSTART
  125.     case SYN_RECEIVED:
  126. #else
  127.     case ESTABLISHED:
  128. #endif
  129.         if((ftp = ftp_create(LINELEN)) == NULLFTP){
  130.             /* No space, kill connection */
  131.             close_tcp(tcb);
  132.             return;
  133.         }
  134.         ftp->control = tcb;        /* Downward link */
  135.         tcb->user = (char *)ftp;    /* Upward link */
  136.  
  137.         /* Set default data port */
  138.         ftp->port.address = tcb->conn.remote.address;
  139.         ftp->port.port = FTPD_PORT;
  140.  
  141.         /* Note current directory */
  142.         log(tcb,"open FTP");
  143.         time(&t);
  144.         cp = ctime(&t);
  145.         if((cp1 = index(cp,'\n')) != NULLCHAR)
  146.             *cp1 = '\0';
  147.         tprintf(ftp->control,banner,hostname,version,cp);
  148.         break;        
  149.     case CLOSE_WAIT:
  150.         close_tcp(tcb);
  151.         break;
  152.     case CLOSED:
  153.         log(tcb,"close FTP");
  154.         if((ftp = (struct ftp *)tcb->user) != NULLFTP)
  155.             ftp_delete(ftp);
  156.         /* Check if server is being shut down */
  157.         if(tcb == ftp_tcb)
  158.             ftp_tcb = NULLTCB;
  159.         del_tcp(tcb);
  160.         break;
  161.     }
  162. }
  163.  
  164. /* FTP Server Control channel Receiver upcall handler */
  165. static
  166. void
  167. ftpscr(tcb,cnt)
  168. struct tcb *tcb;
  169. int16 cnt;
  170. {
  171.     register struct ftp *ftp;
  172.     char c;
  173.     struct mbuf *bp;
  174.     void ftpcommand();
  175.  
  176.     if((ftp = (struct ftp *)tcb->user) == NULLFTP){
  177.         /* Unknown connection, just kill it */
  178.         close_tcp(tcb);
  179.         return;
  180.     }
  181.     switch(ftp->state){
  182.     case COMMAND_STATE:
  183.         /* Assemble an input line in the session buffer. Return if incomplete */
  184.         recv_tcp(tcb,&bp,0);
  185.         while(pullup(&bp,&c,1) == 1){
  186.             switch(c){
  187.             case '\r':    /* Strip cr's */
  188.                 continue;
  189.             case '\n':    /* Complete line; process it */
  190.                 ftp->buf[ftp->cnt] = '\0';
  191.                 ftpcommand(ftp);
  192.                 ftp->cnt = 0;
  193.                 break;
  194.             default:    /* Assemble line */
  195.                 if(ftp->cnt != LINELEN-1)
  196.                     ftp->buf[ftp->cnt++] = c;
  197.                 break;
  198.             }
  199.         }
  200.         /* else no linefeed present yet to terminate command */
  201.         break;
  202.     case SENDING_STATE:
  203.     case RECEIVING_STATE:
  204.         /* Leave commands pending on receive queue until
  205.          * present command is done
  206.          */
  207.         break;
  208.     }
  209. }
  210.  
  211. /* FTP server data channel connection state change upcall handler */
  212. void
  213. ftpsds(tcb,old,new)
  214. struct tcb *tcb;
  215. char old,new;
  216. {
  217.     register struct ftp *ftp;
  218.  
  219.     if((ftp = (struct ftp *)tcb->user) == NULLFTP){
  220.         /* Unknown connection. Kill it */
  221.         del_tcp(tcb);
  222.     } else if((old == FINWAIT1 || old == CLOSING) && ftp->state == SENDING_STATE){
  223.         /* We've received an ack of our FIN while sending; we're done */
  224.         ftp->state = COMMAND_STATE;
  225.         tprintf(ftp->control,txok);
  226.         /* Kick command parser if something is waiting */
  227.         if(ftp->control->rcvcnt != 0)
  228.             ftpscr(ftp->control,ftp->control->rcvcnt);
  229.     } else if(ftp->state == RECEIVING_STATE && new == CLOSE_WAIT){
  230.         /* FIN received on incoming file */
  231. #ifdef    CPM
  232.         if(ftp->type == ASCII_TYPE)
  233.             putc(CTLZ,ftp->fp);
  234. #endif
  235.         close_tcp(tcb);
  236.         if(ftp->fp != stdout)
  237.             fclose(ftp->fp);
  238.         ftp->fp = NULLFILE;
  239.         ftp->state = COMMAND_STATE;
  240.         tprintf(ftp->control,rxok);
  241.         /* Kick command parser if something is waiting */
  242.         if(ftp->control->rcvcnt != 0)
  243.             ftpscr(ftp->control,ftp->control->rcvcnt);
  244.     } else if(new == CLOSED){
  245.         if(old == SYN_SENT){
  246.             /* Data connection was refused, complain about it */
  247.             tprintf(ftp->control,noconn);
  248.             /* And clean up */
  249.             if(ftp->fp != NULLFILE && ftp->fp != stdout)
  250.                 fclose(ftp->fp);
  251.             ftp->fp = NULLFILE;
  252.             ftp->state = COMMAND_STATE;
  253.             /* Kick command parser if something is waiting */
  254.             if(ftp->control->rcvcnt != 0)
  255.                 ftpscr(ftp->control,ftp->control->rcvcnt);
  256.         }
  257.         /* Clear only if another transfer hasn't already started */
  258.         if(ftp->data == tcb)
  259.             ftp->data = NULLTCB;
  260.         del_tcp(tcb);
  261.     }
  262. }
  263.  
  264. /* Parse and execute ftp commands */
  265. static
  266. void
  267. ftpcommand(ftp)
  268. register struct ftp *ftp;
  269. {
  270.     void ftpdr(),ftpdt(),ftpsds();
  271.     char *cmd,*arg,*cp,**cmdp,*file;
  272.     char *pathname();
  273.     char *mode;
  274.     struct socket dport;
  275.     
  276. #ifndef    CPM
  277.     FILE *dir();
  278. #endif
  279.  
  280.     cmd = ftp->buf;
  281.     if(ftp->cnt == 0){
  282.         /* Can't be a legal FTP command */
  283.         tprintf(ftp->control,badcmd);
  284.         return;
  285.     }    
  286.     cmd = ftp->buf;
  287.  
  288. #ifdef    UNIX
  289.     /* Translate first word to lower case */
  290.     for(cp = cmd;*cp != ' ' && *cp != '\0';cp++)
  291.         *cp = tolower(*cp);
  292. #else
  293.     /* Translate entire buffer to lower case */
  294.     for(cp = cmd;*cp != '\0';cp++)
  295.         *cp = tolower(*cp);
  296. #endif
  297.     /* Find command in table; if not present, return syntax error */
  298.     for(cmdp = commands;*cmdp != NULLCHAR;cmdp++)
  299.         if(strncmp(*cmdp,cmd,strlen(*cmdp)) == 0)
  300.             break;
  301.     if(*cmdp == NULLCHAR){
  302.         tprintf(ftp->control,badcmd);
  303.         return;
  304.     }
  305.     /* Allow only USER, PASS and QUIT before logging in */
  306.     if(ftp->cd == NULLCHAR || ftp->path == NULLCHAR){
  307.         switch(cmdp-commands){
  308.         case USER_CMD:
  309.         case PASS_CMD:
  310.         case QUIT_CMD:
  311.             break;
  312.         default:
  313.             tprintf(ftp->control,notlog);
  314.             return;
  315.         }
  316.     }
  317.     arg = &cmd[strlen(*cmdp)];
  318.     while(*arg == ' ')
  319.         arg++;
  320.  
  321.     /* Execute specific command */
  322.     switch(cmdp-commands){
  323.     case USER_CMD:
  324.         if((ftp->username = malloc((unsigned)strlen(arg)+1)) == NULLCHAR){
  325.             close_tcp(ftp->control);
  326.             break;
  327.         }
  328.         strcpy(ftp->username,arg);
  329.         tprintf(ftp->control,givepass);
  330.         break;
  331.     case TYPE_CMD:
  332.         switch(arg[0]){
  333.         case 'A':
  334.         case 'a':    /* Ascii */
  335.             ftp->type = ASCII_TYPE;
  336.             tprintf(ftp->control,typeok);
  337.             break;
  338.         case 'B':
  339.         case 'b':    /* Binary */
  340.         case 'I':
  341.         case 'i':    /* Image */
  342.             ftp->type = IMAGE_TYPE;
  343.             tprintf(ftp->control,typeok);
  344.             break;
  345.         default:    /* Invalid */
  346.             tprintf(ftp->control,badtype,arg);
  347.             break;
  348.         }
  349.         break;
  350.     case QUIT_CMD:
  351.         tprintf(ftp->control,bye);
  352.         close_tcp(ftp->control);
  353.         break;
  354.     case RETR_CMD:
  355.         /* Disk operation; return ACK now */
  356.         tcp_output(ftp->control);
  357.         file = pathname(ftp->cd,arg);
  358.         if(ftp->type == IMAGE_TYPE)
  359.             mode = binmode[READ_BINARY];
  360.         else
  361.             mode = "r";
  362.         if(!permcheck(ftp,RETR_CMD,file)){
  363.              tprintf(ftp->control,noperm);
  364.         } else if((ftp->fp = fopen(file,mode)) == NULLFILE){
  365.             tprintf(ftp->control,cantopen,file);
  366.         } else {
  367.             log(ftp->control,"RETR %s\\%s",ftp->cd,arg); /* DG2KK: was "7" */
  368.             dport.address = ip_addr;
  369.             dport.port = FTPD_PORT;
  370.             ftp->state = SENDING_STATE;
  371.             tprintf(ftp->control,sending,"RETR",arg);
  372.             ftp->data = open_tcp(&dport,&ftp->port,TCP_ACTIVE,
  373.              0,NULLVFP,ftpdt,ftpsds,ftp->control->tos,(char *)ftp);
  374.         }
  375.         free(file);
  376.         break;
  377.     case STOR_CMD:
  378.         /* Disk operation; return ACK now */
  379.         tcp_output(ftp->control);
  380.         file = pathname(ftp->cd,arg);
  381.         if(ftp->type == IMAGE_TYPE)
  382.             mode = binmode[WRITE_BINARY];
  383.         else
  384.             mode = "w";
  385.         if(!permcheck(ftp,STOR_CMD,file)){
  386.              tprintf(ftp->control,noperm);
  387.             free(file);
  388.              break;
  389.         } else if((ftp->fp = fopen(file,mode)) == NULLFILE){
  390.             tprintf(ftp->control,cantmake,file);
  391.         } else {
  392.             log(ftp->control,"STOR %s\\%s",ftp->cd,arg); /* DG2KK */
  393.             dport.address = ip_addr;
  394.             dport.port = FTPD_PORT;
  395.             ftp->state = RECEIVING_STATE;
  396.             tprintf(ftp->control,sending,"STOR",arg);
  397.             ftp->data = open_tcp(&dport,&ftp->port,TCP_ACTIVE,
  398.              0,ftpdr,NULLVFP,ftpsds,ftp->control->tos,(char *)ftp);
  399.         }
  400.         free(file);
  401.         break;
  402.     case PORT_CMD:
  403.         if(pport(&ftp->port,arg) == -1){
  404.             tprintf(ftp->control,badport);
  405.         } else {
  406.             tprintf(ftp->control,portok);
  407.         }
  408.         break;
  409. #ifndef CPM
  410.     case LIST_CMD:
  411.         /* Disk operation; return ACK now */
  412.         tcp_output(ftp->control);
  413.  
  414.         file = pathname(ftp->cd,arg);
  415.         if(!permcheck(ftp,RETR_CMD,file)){
  416.              tprintf(ftp->control,noperm);
  417.         } else if((ftp->fp = dir(file,1)) == NULLFILE){
  418.             tprintf(ftp->control,nodir,file);
  419.         } else {
  420.             dport.address = ip_addr;
  421.             dport.port = FTPD_PORT;
  422.             ftp->state = SENDING_STATE;
  423.             tprintf(ftp->control,sending,"LIST",file);
  424.             ftp->data = open_tcp(&dport,&ftp->port,TCP_ACTIVE,
  425.              0,NULLVFP,ftpdt,ftpsds,ftp->control->tos,(char *)ftp);
  426.         }
  427.         free(file);
  428.         break;
  429.     case NLST_CMD:
  430.         /* Disk operation; return ACK now */
  431.         tcp_output(ftp->control);
  432.  
  433.         file = pathname(ftp->cd,arg);
  434.         if(!permcheck(ftp,RETR_CMD,file)){
  435.              tprintf(ftp->control,noperm);
  436.         } else if((ftp->fp = dir(file,0)) == NULLFILE){
  437.             tprintf(ftp->control,nodir,file);
  438.         } else {
  439.             dport.address = ip_addr;
  440.             dport.port = FTPD_PORT;
  441.             ftp->state = SENDING_STATE;
  442.             tprintf(ftp->control,sending,"NLST",file);
  443.             ftp->data = open_tcp(&dport,&ftp->port,TCP_ACTIVE,
  444.              0,NULLVFP,ftpdt,ftpsds,ftp->control->tos,(char *)ftp);
  445.         }
  446.         free(file);
  447.         break;
  448.     case CWD_CMD:
  449.         tcp_output(ftp->control);    /* Disk operation; return ACK now */
  450.  
  451.         file = pathname(ftp->cd,arg);
  452.         if(!permcheck(ftp,RETR_CMD,file)){
  453.              tprintf(ftp->control,noperm);
  454.             free(file);
  455. #if ( MSDOS || ATARI_ST )
  456.         /* Don'tcha just LOVE %%$#@!! MS-DOS? */
  457.         } else if(strcmp(file,"\\") == 0 || access(file,0) == 0){
  458. #else
  459.         } else if(access(file,0) == 0){    /* See if it exists */
  460. #endif
  461.             /* Succeeded, record in control block */
  462.             free(ftp->cd);
  463.             ftp->cd = file;
  464.             tprintf(ftp->control,pwdmsg,file);
  465.         } else {
  466.             /* Failed, don't change anything */
  467.             tprintf(ftp->control,nodir,file);
  468.             free(file);
  469.         }
  470.         break;
  471.     case XPWD_CMD:
  472.     case PWD_CMD:
  473.         tprintf(ftp->control,pwdmsg,ftp->cd);
  474.         break;
  475. #else
  476.     case LIST_CMD:
  477.     case NLST_CMD:
  478.     case CWD_CMD:
  479.     case XPWD_CMD:
  480.     case PWD_CMD:
  481. #endif
  482.     case ACCT_CMD:        
  483.         tprintf(ftp->control,unimp);
  484.         break;
  485.     case DELE_CMD:
  486.         file = pathname(ftp->cd,arg);
  487.         if(!permcheck(ftp,DELE_CMD,file)){
  488.              tprintf(ftp->control,noperm);
  489.         } else if(unlink(file) == 0){
  490.             tprintf(ftp->control,deleok);
  491.         } else {
  492.             tprintf(ftp->control,delefail);
  493.         }
  494.         free(file);
  495.         break;
  496.     case PASS_CMD:
  497.         tcp_output(ftp->control);    /* Send the ack now */
  498.         ftplogin(ftp,arg);            
  499.         break;
  500. #ifndef    CPM
  501.     case XMKD_CMD:
  502.     case MKD_CMD:
  503.         file = pathname(ftp->cd,arg);
  504.         if(!permcheck(ftp,MKD_CMD,file)){
  505.             tprintf(ftp->control,noperm);
  506.         } else if(mkdir(file,0777) == 0){
  507.             tprintf(ftp->control,mkdok);
  508.         } else {
  509.             tprintf(ftp->control,cantmake);
  510.         }
  511.         free(file);
  512.         break;
  513.     case XRMD_CMD:
  514.     case RMD_CMD:
  515.         file = pathname(ftp->cd,arg);
  516.         if(!permcheck(ftp,RMD_CMD,file)){
  517.              tprintf(ftp->control,noperm);
  518.         } else if(rmdir(file) == 0){
  519.             tprintf(ftp->control,deleok);
  520.         } else {
  521.             tprintf(ftp->control,delefail);
  522.         }
  523.         free(file);
  524.         break;
  525.     }
  526. #endif
  527. }
  528. static
  529. int
  530. pport(sock,arg)
  531. struct socket *sock;
  532. char *arg;
  533. {
  534.     int32 n;
  535.     int atoi(),i;
  536.  
  537.     n = 0;
  538.     for(i=0;i<4;i++){
  539.         n = atoi(arg) + (n << 8);
  540.         if((arg = index(arg,',')) == NULLCHAR)
  541.             return -1;
  542.         arg++;
  543.     }
  544.     sock->address = n;
  545.     n = atoi(arg);
  546.     if((arg = index(arg,',')) == NULLCHAR)
  547.         return -1;
  548.     arg++;
  549.     n = atoi(arg) + (n << 8);
  550.     sock->port = n;
  551.     return 0;
  552. }
  553. /* Attempt to log in the user whose name is in ftp->username and password
  554.  * in pass
  555.  */
  556. static
  557. ftplogin(ftp,pass)
  558. struct ftp *ftp;
  559. char *pass;
  560. {
  561.     char buf[80],*cp,*cp1;
  562.     FILE *fp;
  563.     int anony = 0;
  564.  
  565.     if((fp = fopen(userfile,"r")) == NULLFILE){
  566.         /* Userfile doesn't exist */
  567.         tprintf(ftp->control,noperm);
  568.         return;
  569.     }
  570.     while(fgets(buf,sizeof(buf),fp),!feof(fp)){
  571.         if(buf[0] == '#')
  572.             continue;    /* Comment */
  573.         if((cp = index(buf,' ')) == NULLCHAR)
  574.             /* Bogus entry */
  575.             continue;
  576.         *cp++ = '\0';        /* Now points to password */
  577.         if(strcmp(ftp->username,buf) == 0)
  578.             break;        /* Found user name */
  579.     }
  580.     if(feof(fp)){
  581.         /* User name not found in file */
  582.         fclose(fp);
  583.         tprintf(ftp->control,noperm);
  584.         return;
  585.     }
  586.     fclose(fp);
  587.     /* Look for space after password field in file */
  588.     if((cp1 = index(cp,' ')) == NULLCHAR){
  589.         /* Invalid file entry */
  590.         tprintf(ftp->control,noperm);
  591.         return;
  592.     }
  593.     *cp1++ = '\0';    /* Now points to path field */
  594.     if(strcmp(cp,"*") == 0)
  595.         anony = 1;    /* User ID is password-free */
  596.     if(!anony && strcmp(cp,pass) != 0){
  597.         /* Password required, but wrong one given */
  598.         tprintf(ftp->control,noperm);
  599.         return;
  600.     }
  601.     if((cp = index(cp1,' ')) == NULLCHAR){
  602.         /* Permission field missing */
  603.         tprintf(ftp->control,noperm);
  604.         return;
  605.     }
  606.     *cp++ = '\0';    /* now points to permission field */
  607.  
  608.     /* Set up current directory and path prefix */
  609.     ftp->cd = malloc((unsigned)strlen(cp1)+1);
  610.     ftp->path = malloc((unsigned)strlen(cp1)+1);
  611.     strcpy(ftp->cd,cp1);
  612.     strcpy(ftp->path,cp1);
  613.     
  614.     /* And finally set the permission bits */
  615.     ftp->perms = atoi(cp);
  616.  
  617.     tprintf(ftp->control,logged);
  618.     if(!anony)
  619.         log(ftp->control,"%s logged in",ftp->username);
  620.     else
  621.         log(ftp->control,"%s logged in, ID %s",ftp->username,pass);
  622. }        
  623.  
  624. #if ( MSDOS || ATARI_ST )
  625. /* Illegal characters in a DOS filename */
  626. char badchars[] = "\"[]:|<>+=;,";
  627. #endif
  628.  
  629. /* Return 1 if the file operation is allowed, 0 otherwise */
  630. permcheck(ftp,op,file)
  631. struct ftp *ftp;
  632. int op;
  633. char *file;
  634. {
  635.     char *cp;
  636.     FILE *fp;    /* DG2KK */
  637.  
  638.     if(file == NULLCHAR || ftp->path == NULLCHAR)
  639.         return 0;    /* Probably hasn't logged in yet */
  640. #if ( MSDOS || ATARI_ST )
  641.     /* Check for characters illegal in MS-DOS file names */
  642.     for(cp = badchars;*cp != '\0';cp++){
  643.         if(index(file,*cp) != NULLCHAR)
  644.             return 0;    
  645.     }
  646. #endif
  647. #if (!AMIGA && !MAC)
  648.     /* The target file must be under the user's allowed search path */
  649.     if(strncmp(file,ftp->path,strlen(ftp->path)) != 0)
  650.         return 0;
  651. #endif
  652.  
  653.     switch(op){
  654.     case RETR_CMD:
  655.         /* User must have permission to read files */
  656.         if(ftp->perms & FTP_READ)
  657.             return 1;
  658.         return 0;
  659.     case DELE_CMD:
  660.     case RMD_CMD:
  661.         /* User must have permission to (over)write files */
  662.         if(ftp->perms & FTP_WRITE)
  663.             return 1;
  664.         return 0;
  665.     case STOR_CMD:
  666.     case MKD_CMD:
  667.         /* User must have permission to (over)write files, or permission
  668.          * to create them if the file doesn't already exist
  669.          */
  670.         if(ftp->perms & FTP_WRITE)
  671.             return 1;
  672. #ifdef LATTICE
  673.         /* DG2KK: Lattice's access() doesn't work.
  674.          */
  675.         fp = fopen(file,"r");
  676.         if(fp == NULLFILE && (ftp->perms & FTP_CREATE)) {
  677. #else
  678.         if(access(file,2) == -1 && (ftp->perms & FTP_CREATE)) {
  679. #endif
  680.             return 1;
  681.         }
  682. #ifdef LATTICE
  683.         fclose(fp);
  684. #endif
  685.         return 0;
  686.     }
  687.     return 0;    /* "can't happen" -- keep lint happy */
  688. }
  689.